//
//  GQHContractExpenseInfoView.m
//  Xanthium
//
//  Created by GuanQinghao on 18/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHContractExpenseInfoView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"


@interface GQHContractExpenseInfoView () <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate> {
    
    NSArray *rowTitleArray;
    NSArray *sectionTitleArray;
    NSArray *_perArray;
    NSArray *_placeHolderArray;
}

@end


@implementation GQHContractExpenseInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData
{
    sectionTitleArray = @[@"货物信息",@"预付费",@"尾款",@"运费金额"];
    rowTitleArray = @[@"货物重量",@"报价金额",@"油卡",@"ETC",@"现金",@"预付款合计",@"油卡",@"ETC",@"现金",@"尾款合计",@"运费总计"];
    _qh_contentArray = [NSMutableArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"", nil];
    _perArray = @[@"吨",@"元/吨",@"元",@"元",@"元",@"元",@"元",@"元",@"元",@"元",@"元"];
    _placeHolderArray = @[@"请输入",@"请输入",@"",@"",@"",@"",@"",@"",@"",@"",@""];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return sectionTitleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
            
        case 0:
            return 2;
            break;
            
        case 1:
            return 4;
            break;
            
        case 2:
            return 4;
            break;
        case 3:
            return 1;
            break;
            
        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.isEnter = YES;
    cell.enterText.delegate = self;
    cell.enterText.keyboardType = UIKeyboardTypeDecimalPad;
    switch (indexPath.section) {
            
        case 0:
        {
            cell.titleLabel.text = rowTitleArray[indexPath.row];
            cell.perLabel.text = _perArray[indexPath.row];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row];
            cell.enterText.tag = 10 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row];
            if (indexPath.row == 1)
            {
                cell.enterText.enabled = NO;
            }
        }
            break;
        case 1: {
            
            cell.titleLabel.text = rowTitleArray[indexPath.row + 2];
            cell.perLabel.text = _perArray[indexPath.row + 2];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row + 2];
            cell.enterText.tag = 20 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row + 2];
            
            if (indexPath.row == 3)
            {
                cell.enterText.enabled = NO;
            }
        }
            break;
        case 2: {
            
            cell.titleLabel.text = rowTitleArray[indexPath.row + 6];
            cell.perLabel.text = _perArray[indexPath.row + 6];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row + 6];
            cell.enterText.tag = 30 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row + 6];
            if (indexPath.row == 3)
            {
                cell.enterText.enabled = NO;
            }
        }
            break;
        case 3: {
            
            cell.titleLabel.text = rowTitleArray[indexPath.row + 10];
            cell.perLabel.text = _perArray[indexPath.row + 10];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row + 10];
            cell.enterText.tag = 40 + indexPath.row;
            cell.enterText.enabled = NO;
            cell.enterText.text = _qh_contentArray[indexPath.row + 10];
        }
            break;
        default:
            
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
//
//        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = sectionTitleArray[section];
    titleLabel.textColor = kAppRedColor;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(headerView);
        make.left.and.right.mas_equalTo(headerView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSInteger tag = textField.tag / 10;
    switch (tag) {
        case 1:
            [_qh_contentArray replaceObjectAtIndex:textField.tag - 10 withObject:textField.text];
            break;
        case 2:
        {
            [_qh_contentArray replaceObjectAtIndex:textField.tag - 20 + 2 withObject:textField.text];
            
            UITextField * total = (UITextField *)[self.qh_tableView viewWithTag:23];
            total.text = [NSString stringWithFormat:@"%.2f", [_qh_contentArray[2] floatValue] + [_qh_contentArray[3] floatValue] + [_qh_contentArray[4] floatValue]];
            [_qh_contentArray replaceObjectAtIndex:5 withObject:total.text];
        }
            break;
        case 3:
        {
            [_qh_contentArray replaceObjectAtIndex:textField.tag - 30 + 6 withObject:textField.text];
            
            UITextField * total = (UITextField *)[self.qh_tableView viewWithTag:33];
            total.text = [NSString stringWithFormat:@"%.2f", [_qh_contentArray[6] floatValue] + [_qh_contentArray[7] floatValue] + [_qh_contentArray[8] floatValue]];
            [_qh_contentArray replaceObjectAtIndex:9 withObject:total.text];
        }
            break;
        case 4:
            [_qh_contentArray replaceObjectAtIndex:10 withObject:textField.text];
            break;
            
        default:
            break;
    }
    UITextField * total = (UITextField *)[self.qh_tableView viewWithTag:40];
    if (total)
    {
        total.text = [NSString stringWithFormat:@"%.2f", [_qh_contentArray[5] floatValue] + [_qh_contentArray[9] floatValue]];
        [_qh_contentArray replaceObjectAtIndex:10 withObject:total.text];
    }
}

@end
